1
2
3
4
5
6
7 package uk.ac.roe.antigen.ant;
8
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Enumeration;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Hashtable;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.Target;
25 import org.apache.tools.ant.Task;
26
27 /***
28 * Given a starting target, traverse the tree
29 * and work out the total number of tasks and targets to be run
30 * @author jdt
31 */
32 public class TaskTreeCalcuator {
33 /***
34 * Logger for this class
35 */
36 private static final Logger logger = Logger.getLogger(TaskTreeCalcuator.class.getName());
37
38
39 Map namesToTargets = new HashMap();
40 private Hashtable targets;
41 public TaskTreeCalcuator(Project project) {
42 targets = project.getTargets();
43 }
44
45 public Set getUniqueDependencies(final Target root) {
46
47 Enumeration dependencies = root.getDependencies();
48 Set totalDependencies = new HashSet();
49 while (dependencies.hasMoreElements()) {
50 String dependencyName = (String) dependencies.nextElement();
51 Target dependency = (Target) targets.get(dependencyName);
52 totalDependencies.add(dependency);
53 totalDependencies.addAll(getUniqueDependencies(dependency));
54 }
55 return totalDependencies;
56 }
57
58 public int getNumberOfTargetsToRun(final Target root) {
59 return getUniqueDependencies(root).size()+1;
60 }
61
62 public List getTasksToRun(final Target root) {
63 Set targets = getUniqueDependencies(root);
64 Iterator it = targets.iterator();
65 List allTasks = new ArrayList();
66 allTasks.addAll(Arrays.asList(root.getTasks()));
67 while (it.hasNext()) {
68
69 Target target = (Target) it.next();
70 Task[] tasks = target.getTasks();
71 logger.fine("getTasksToRun(Target) - target " + target.getName()
72 + " has " + tasks.length + " tasks");
73 allTasks.addAll(Arrays.asList(tasks));
74 }
75 return allTasks;
76 }
77
78 public int getNumberOfTasksToRun(final Target root) {
79 return getTasksToRun(root).size();
80 }
81
82 /***
83 * @param defaultTargetName
84 * @return
85 */
86 public int getNumberOfTasksToRun(String defaultTargetName) {
87 Target target = (Target) targets.get(defaultTargetName);
88 return getNumberOfTasksToRun(target);
89 }
90 }